<template>
	<view>
		<!-- 顶部导航 -->
		<view class="topTabBar" :style="{position:headerPosition,top:headerTop}">
			<view class="grid" v-for="(grid,tbIndex) in orderType" :key="tbIndex" @tap="showType(tbIndex)">
				<view class="text" :class="[tbIndex==tabbarIndex?'on':'']">{{grid}}</view>
			</view>
		</view>
		<!-- 考虑非APP端长列表和复杂的DOM使用scroll-view会卡顿，所以漂浮顶部选项卡使用page本身的滑动 -->
		<view class="order-list">
			<view class="list">
				<view class="onorder" v-if="list.length==0">
					<image src="../../../static/img/noorder.png"></image>
					<view class="text">
						没有相关订单
					</view>
				</view>
				<view class="row" v-for="(row,index) in list" :key="index">
					<view class="type">
						<view>※  {{typeText[row.type]}}</view>
						<view style="margin-left: 20upx;">{{row.orderStartTime}}</view>
					</view>
					<view class="order-info" v-for="(good,index) in row.goodList" :key="index">
						<view class="left">
							<image :src="good.img"></image>
						</view>
						<view class="right">
							<view class="name">
								{{good.name}}
							</view>
							<view class="spec">{{good.spec}}</view>
							<view class="price-number">
								<view class="price">{{good.price}}</view>
								x<view class="number">{{good.numner}}</view>
							</view>
							<view class="detail" v-if="index+1 < row.goodList.length">
								<view class="number">购买{{good.numner}}件商品</view>
							</view>
							<view class="detail" v-else>
								<view class="number">购买{{good.numner}}件商品</view>
								<view class="sum">合计￥<view class="price">{{good.payment}}</view></view>
								<view class="nominal">(含运费 ￥{{good.freight}})</view>
							</view>
						</view>
					</view>
					<view class="btns">
						<block v-if="row.type=='unpaid'"><view class="default">取消订单</view><view class="pay" @tap="toPayment(row)">付款</view></block>
						<block v-if="row.type=='back'"><view class="default">提醒发货</view></block>
						<block v-if="row.type=='unreceived'"><view class="default">查看物流</view><view class="pay">确认收货</view><view class="pay">我要退货</view></block>
						<block v-if="row.type=='received'"><view class="default">评价</view><view class="default">再次购买</view></block>
						<block v-if="row.type=='completed'"><view class="default">再次购买</view></block>
						<block v-if="row.type=='refunds'"><view class="default">查看进度</view></block>

					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	// 封装接口调用
	import orderList from '../../../pagesUser/request/pages/user/order/order';
	
	export default {
		data() {
			return {
				headerPosition:"fixed",
				headerTop:"0px",
				typeText:{
					unpaid:'等待付款',
					back:'等待商家发货',
					unreceived:'商家已发货',
					received:'等待用户评价',
					completed:'交易已完成',
					refunds:'商品退货处理中'
				},
				orderType: ['全部','待付款','待发货','待收货','待评价','退换货'],
				//订单列表 演示数据
				orderList:[
					[
						{ 
							type:"unpaid",
							ordersn:0,
							orderStartTime: '2021-11-12 12:12',
							goodList: [
								{
									goods_id: 0,
									img: '/static/img/goods/p1.jpg', 
									name: '商品名称商品名称商品名称商品名称商品名称', 
									price: '168.00',
									payment:168.00,
									freight:12.00,
									spec:'规格:S码',
									numner:1
								},
								{
									goods_id: 0,
									img: '/static/img/goods/p1.jpg', 
									name: '商品名称商品名称商品名称商品名称商品名称', 
									price: '168.00',
									payment:168.00,
									freight:12.00,
									spec:'规格:S码',
									numner:1
								}
							]
						},
						{type:"back",ordersn:1,orderStartTime: '2021-11-12 12:12',goodList: [{goods_id: 0,img: '/static/img/goods/p1.jpg',name: '商品名称商品名称商品名称商品名称商品名称',price: '168.00',payment:168.00,freight:12.00,spec:'规格:S码',numner:1}]},
						{type:"unreceived",ordersn:1,orderStartTime: '2021-11-12 12:12',goodList: [{goods_id: 0,img: '/static/img/goods/p1.jpg',name: '商品名称商品名称商品名称商品名称商品名称',price: '168.00',payment:168.00,freight:12.00,spec:'规格:S码',numner:1}]},
						{type:"received",ordersn:1,orderStartTime: '2021-11-12 12:12',goodList: [{goods_id: 0,img: '/static/img/goods/p1.jpg',name: '商品名称商品名称商品名称商品名称商品名称',price: '168.00',payment:168.00,freight:12.00,spec:'规格:S码',numner:1}]},
						{type:"completed",ordersn:1,orderStartTime: '2021-11-12 12:12',goodList: [{goods_id: 0,img: '/static/img/goods/p1.jpg',name: '商品名称商品名称商品名称商品名称商品名称',price: '168.00',payment:168.00,freight:12.00,spec:'规格:S码',numner:1}]},
						{type:"refunds",ordersn:1,orderStartTime: '2021-11-12 12:12',goodList: [{goods_id: 0,img: '/static/img/goods/p1.jpg',name: '商品名称商品名称商品名称商品名称商品名称',price: '168.00',payment:168.00,freight:12.00,spec:'规格:S码',numner:1}]},
					],
					[
						{
							type:"unpaid",
							ordersn:0,
							orderStartTime: '2021-11-12 12:12',
							goodList: [
								{
									goods_id: 0,
									img: '/static/img/goods/p1.jpg', 
									name: '商品名称商品名称商品名称商品名称商品名称', 
									price: '168.00',
									payment:168.00,
									freight:12.00,
									spec:'规格:S码',
									numner:1
								},
								{
									goods_id: 0,
									img: '/static/img/goods/p1.jpg', 
									name: '商品名称商品名称商品名称商品名称商品名称', 
									price: '168.00',
									payment:168.00,
									freight:12.00,
									spec:'规格:S码',
									numner:1
								}
							]
						},
					],
					[
						//无
					],
					[
						{type:"unreceived",ordersn:1,orderStartTime: '2021-11-12 12:12',goodList: [{goods_id: 0,img: '/static/img/goods/p1.jpg',name: '商品名称商品名称商品名称商品名称商品名称',price: '168.00',payment:168.00,freight:12.00,spec:'规格:S码',numner:1}]},],
					[
						{type:"received",ordersn:1,orderStartTime: '2021-11-12 12:12',goodList: [{goods_id: 0,img: '/static/img/goods/p1.jpg',name: '商品名称商品名称商品名称商品名称商品名称',price: '168.00',payment:168.00,freight:12.00,spec:'规格:S码',numner:1}]},
					],
					[
						{type:"refunds",ordersn:1,orderStartTime: '2021-11-12 12:12',goodList: [{goods_id: 0,img: '/static/img/goods/p1.jpg',name: '商品名称商品名称商品名称商品名称商品名称',price: '168.00',payment:168.00,freight:12.00,spec:'规格:S码',numner:1}]},
					]
					
				],
				list:[],
				tabbarIndex:0
			};
		},
		onLoad(option) {
			// 获取当前用户的信息，查询全部的订单信息
			const data = {
				personId: getApp().globalData.userData.personId
			};
			orderList.selectOrderList(data).then(res => {
				// 分离订单信息
				let optionsList = this.getJsonCar(res.data, "orderId");
			
				// 清空list
				this.orderList = [[],[],[],[],[],[]];
			
				// 循环遍历添加
				optionsList.forEach(item => {
					// 商品集合
					let goodList = [];
					item.forEach(good => {
						// 商品编号
						let goods_id = good.commId; 
						// 商品图片
						let img = good.commPicture; 
						// 商品名称 
						let name = good.commName; 
						// 商品单价格
						let price = good.commDanjia;
						// 总金额
						let payment = Number(good.allMoney + 12.00);
						// 服务费用
						let freight = 12.00;
						// 商品规格
						let spec = '规格:普通';
						// 商品数量
						let numner = good.num;
						goodList.push({
							goods_id: goods_id,
							img: img, 
							name: name, 
							price: price,
							payment: payment,
							freight: freight,
							spec: spec,
							numner: numner
						});
					})
					// 格式化时间
					var nowDate = new Date(item[0].orderStartTime);
					var year = nowDate.getFullYear();
					var month = nowDate.getMonth() + 1;
					var day = nowDate.getDate();
					var hour = nowDate.getHours() < 10 ? "0" + nowDate.getHours() : nowDate.getHours();
					var minute = nowDate.getMinutes() < 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes();
					month >= 1 && month <= 9 ? (month = "0" + month) : "";
					day >= 0 && day <= 9 ? (day = "0" + day) : "";
					var nowTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
					
					if(item[0].orderType == '1'){
						let orderData1 = {
							type: "unpaid",
							ordersn: item[0].orderType,
							orderStartTime: nowTime,
							goodList: goodList
						};
						this.orderList[1].push(orderData1);
						this.orderList[0].push(orderData1);
					}else if(item[0].orderType == '2'){
						let orderData2 = {
							type: "back",
							ordersn: item[0].orderType,
							orderStartTime: nowTime,
							goodList: goodList
						};
						this.orderList[2].push(orderData2);
						this.orderList[0].push(orderData2);
					}else if(item[0].orderType == '3'){
						let orderData3 = {
							type: "back",
							ordersn: item[0].orderType,
							orderStartTime: nowTime,
							goodList: goodList
						};
						this.orderList[3].push(orderData3);
						this.orderList[0].push(orderData3);
					}else if(item[0].orderType == '4'){
						let orderData4 = {
							type: "back",
							ordersn: item[0].orderType,
							orderStartTime: nowTime,
							goodList: goodList
						};
						this.orderList[4].push(orderData4);
						this.orderList[0].push(orderData4);
					}else if(item[0].orderType == '5'){
						let orderData5 = {
							type: "back",
							ordersn: item[0].orderType,
							orderStartTime: nowTime,
							goodList: goodList
						};
						this.orderList[5].push(orderData5);
						this.orderList[0].push(orderData5);
					}
				});
				console.log(this.orderList);
			});
			
			//option为object类型，会序列化上个页面传递的参数
			setTimeout(()=>{
				console.log("option: " + JSON.stringify(option));
				let tbIndex = parseInt(option.tbIndex)+1;
				this.list = this.orderList[tbIndex];
				this.tabbarIndex = tbIndex;
			}, 500)
			
			//兼容H5下排序栏位置
			// #ifdef H5
				let Timer = setInterval(()=>{
					let uniHead = document.getElementsByTagName('uni-page-head');
					if(uniHead.length>0){
						this.headerTop = uniHead[0].offsetHeight+'px';
						clearInterval(Timer);//清除定时器
					}
				},1);
			// #endif
		},
		onPageScroll(e){
			return;
			//兼容iOS端下拉时顶部漂移
			this.headerPosition = e.scrollTop>=0?"fixed":"absolute";
		},
		methods: {
			/**
			* 分割数组创建二维数组封装
			* @param Json 数组
			* @param key 需要分割的属性
			*/
			getJsonCar(Json, key){
			    //数据归类方法
			    let kind = []; //存放属性标识
			    let newArr = []; //返回的数据
			    Json.map((item) => {
			        // 判断key是否存在，不存在则添加
			        if (!kind.includes(item[key])) {
						kind.push(item[key]); //kind添加新标识
			            newArr.push([]); //添加数组
			        }
			        let index = kind.indexOf(item[key]); //返回带有标识在kind内的下标，判断加入哪个数组
			        newArr[index].push(item); //将对象存入数组
			    });
			    return newArr;
			},
			showType(tbIndex){
				this.tabbarIndex = tbIndex;
				this.list = this.orderList[tbIndex];
			},
			toPayment(row){
				//本地模拟订单提交UI效果
				uni.showLoading({
					title:'正在获取订单...'
				})
				let paymentOrder = [];
				paymentOrder.push(row);
				setTimeout(()=>{
					uni.setStorage({
						key:'paymentOrder',
						data:paymentOrder,
						success: () => {
							uni.hideLoading();
							uni.navigateTo({
								url:'../../../pages/pay/payment/payment?amount='+row.payment
							})
						}
					})
				},500)
			}
		}
	}
</script>

<style lang="scss">
page{
	background-color: #f3f3f3;
}
.topTabBar{
	width: 100%;
	position: fixed;
	top: 0;
	z-index: 10;
	background-color: #f8f8f8;
	height: 80upx;
	display: flex;
	justify-content: space-around;
	.grid{
		width: 20%;
		height: 80upx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #444;
		font-size: 28upx;
		.text{
			height: 76upx;
			display: flex;
			align-items: center;
			&.on{
				color: #3E8BC7;
				border-bottom: solid 4upx #3E8BC7;
			}
		}
		
	}
}
.order-list{
	margin-top: 80upx;
	padding-top: 20upx;
	width: 100%;
	.list{
		width: 94%;
		margin: 0 auto;
		.onorder{
			width: 100%;
			height: 50vw;
			display: flex;
			justify-content: center;
			align-content: center;
			flex-wrap: wrap;
			image{
				width: 20vw;
				height: 20vw;
				border-radius: 100%;
			}
			.text{
				width: 100%;
				height: 60upx;
				font-size: 28upx;
				color: #444;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
		.row{
			width: calc(100% - 40upx);
			padding: 10upx 20upx;
			border-radius: 10upx;
			background-color: #fff;
			margin-bottom: 20upx;
			.type{
				font-size: 28upx;
				margin-bottom: 10upx;
				color: #3E8BC7;
				height: 50upx;
				display: flex;
				align-items: center;
			}
			.order-info{
				width: 100%;
				display: flex;
				margin-bottom: 30upx;
				.left{
					flex-shrink: 0;
					width: 25vw;
					height: 25vw;
					image{
						width: 25vw;
						height: 25vw;
						border-radius: 10upx;
					}
				}
				.right{
					width: 100%;
					margin-left: 10upx;
					position: relative;
					.name{
						width: 100%;
						font-size: 28upx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
					}
					.spec{
						color: #a7a7a7;
						font-size: 22upx;

					}
					.price-number{
						bottom: 0;
						width: 100%;
						font-size: 22upx;
						color: #333;
						display: flex;
						justify-content: flex-end;
						align-items: flex-end;
						.price{
							font-size: 24upx;
							margin-right: 5upx;
						}
						
					}
					.detail{
						display: flex;
						justify-content: flex-end;
						align-items: flex-end;
						height: 60upx;
						font-size: 26upx;
						.sum{
							padding: 0 8upx;
							display: flex;
							align-items: flex-end;
							.price{
								font-size: 30upx;
							}
						}
						
					}
				}
			}
			.btns{
				height: 80upx;
				display: flex;
				align-items: center;
				justify-content: flex-end;
				view{
					min-width: 120upx;
					height: 50upx;
					padding: 0 20upx;
					border-radius: 50upx;
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 28upx;
					margin-left: 20upx;
				}
				.default{
					border: solid 1upx #ccc;
					color: #666;
				}
				.pay{
					border: solid 1upx #3E8BC7;
					color: #3E8BC7;
				}
			}
		}
	}
}
</style>
